package member;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import book.RentalBean;

import common.DBManager;
import common.TableManager;

public class RentalListServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		doGet(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");

		boolean restriction;

		Calendar cal=Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		Date date=cal.getTime();

		String tomorrow=new SimpleDateFormat("yyyy-MM-dd").format(date);

		System.out.println(tomorrow);

		if(req.getParameter("checker")==null)
		{
			restriction=false;
			//System.out.println("checker-null");
		}
		else
		{
			restriction=true;
			req.setAttribute("checker", "unchecked");
			//System.out.println("checker-notNull");
		}

		// ページ設定
		String pageStr = req.getParameter("page");
		int page;
		int max = 10;	// 1ページあたりの表示件数
		if(pageStr == null)
		{
			page = 1;
		}
		else
		{
			page = Integer.parseInt(pageStr);
		}
		int start = (page - 1) * max;

		String sql =null;
		String sqlCnt =null;

		if(!restriction)
		{
			System.out.println("showAll");
			sql = "select * from rentals order by id limit " + start + ", " + max;
			sqlCnt = "select count(*) as cnt from rentals";
			System.out.println(sql+"♡"+sqlCnt);
		}
		else
		{
			System.out.println("showPart");
			sql = "select * from rentals where return_plan_date = '"+tomorrow+"' order by id limit " + start + ", " + max;
			sqlCnt = "select count(*) as cnt from rentals where return_plan_date = '"+tomorrow+"'";
		}

		Connection con = null;
		Statement smt = null;

		try
		{
			con = DBManager.getConnection();
			smt = con.createStatement();
			ResultSet rs = smt.executeQuery(sql);

			List<RentalBean> list = new ArrayList<RentalBean>();
//			List<MemberBean> listAppend = new ArrayList<MemberBean>();
//			List<BookBean> listAppend2=new ArrayList<BookBean>();

			while(rs.next())
			{
				System.out.println(rs.getInt("ID"));
				RentalBean rentals=(RentalBean)TableManager.getRecord(TableManager.RENTALS_TABLE, rs.getInt("ID")).getBean();

				list.add(rentals);
			}

			req.setAttribute("list", list);

			rs = smt.executeQuery(sqlCnt);
			rs.next();
			Integer cnt = new Integer(rs.getInt("cnt"));
			req.setAttribute("cnt", cnt);

			if(cnt==0)
			{
				throw new NullPointerException();
			}

		}
		catch(NullPointerException e)
		{
			//e.printStackTrace();
			req.setAttribute("list", null);
		}
		catch(SQLException e)
		{
			throw new ServletException(e);
		}
		finally
		{
			if(smt != null)
			{
				try
				{
					smt.close();
				}
				catch(SQLException ignore)
				{
					// none
				}
			}
			if(con != null)
			{
				try
				{
					con.close();
				}
				catch(SQLException ignore)
				{
					// none
				}
			}

			req.setAttribute("page", page);
			req.getRequestDispatcher("/member/userMemberNotReturn.jsp").forward(req, resp);
		}
	}
}
